package api;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.Blog;
import dao.BlogDao;
import dao.User;
import dao.UserDao;

@WebServlet("/blog_de")
public class Blog_message extends HttpServlet {

    @Override
    //销户
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if(user.getPassword().equals(req.getParameter("password"))&&user.getUsername().equals(req.getParameter("username")))
        {
            UserDao userDao=new UserDao();
            userDao.delet(user);
            session.removeAttribute("user");
            resp.sendRedirect("./html/login.html");
        }
       else{
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户名或密码者错误");
        }
    }

//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String blogId = req.getParameter("blogId");
//
//        if (blogId == null) {
//            // 博客列表页
//            // 从 session 中拿到 user 对象.
//            HttpSession session = req.getSession(false);
//            if (session == null) {
//                User user = new User();
//                String respJson = objectMapper.writeValueAsString(user);
//                resp.setContentType("application/json; charset=utf8");
//                resp.getWriter().write(respJson);
//                return;
//            }
//            User user = (User) session.getAttribute("user");
//            if (user == null) {
//                user = new User();
//                String respJson = objectMapper.writeValueAsString(user);
//                resp.setContentType("application/json; charset=utf8");
//                resp.getWriter().write(respJson);
//                return;
//            }
//            String respJson = objectMapper.writeValueAsString(user);
//            resp.setContentType("application/json; charset=utf8");
//            resp.getWriter().write(respJson);
//        } else {
//            // 博客详情页
//            // 需要查询数据库了.
//            //1.获取当前用户的序号
//            HttpSession session = req.getSession();
//            User user = (User) session.getAttribute("user");
//            //2.查询当前用户指定的博客序号
//            BlogDao blogDao = new BlogDao();
//            Blog blog = blogDao.getBlog(Integer.parseInt(blogId),user.getUserId());
//            String respJson = objectMapper.writeValueAsString(blog);
//                resp.setContentType("application/json; charset=utf8");
//                resp.getWriter().write(respJson);
//
////            if (blog == null) {
////                User user = new User();
////                String respJson = objectMapper.writeValueAsString(user);
////                resp.setContentType("application/json; charset=utf8");
////                resp.getWriter().write(respJson);
////                return;
////            }
////
////            if (user == null) {
////                user = new User();
////                String respJson = objectMapper.writeValueAsString(user);
////                resp.setContentType("application/json; charset=utf8");
////                resp.getWriter().write(respJson);
////                return;
////            }
////            String respJson = objectMapper.writeValueAsString(user);
////            resp.setContentType("application/json; charset=utf8");
////            resp.getWriter().write(respJson);
//        }
//    }
}